import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.Test;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestConn {

    private static final String url = "jdbc:t4jdbc://10.11.30.61:23400/:catalog=TRAFODION;applicationName=testClose";
    private static final String usr = "db__root";
    private static final String pwd = "traf123";
    private static final String driver = "org.trafodion.jdbc.t4.T4Driver";


    public static void main(String[] args) {
        try {
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url, usr, pwd);

            PreparedStatement statement = conn.prepareStatement("SELECT * FROM test001 t1 LEFT \n" +
                    "JOIN test001 t2 ON t2.id > t1.id\n" +
                    "JOIN test001 t3 ON t3.id < t2.id\n" +
                    "JOIN test001 t4 ON t4.id < t3.id\n" +
                    "JOIN test001 t5 ON t5.id < t4.id\n" +
                    "JOIN test001 t6 ON t5.id < t4.id\n" +
                    "JOIN test001 t7 ON t5.id < t4.id\n" +
                    "JOIN test001 t8 ON t5.id < t4.id\n" +
                    "JOIN test001 t9 ON t5.id < t4.id\n" +
                    "JOIN test001 t10 ON t5.id < t4.id\n" +
                    "JOIN test001 t11 ON t5.id < t4.id\n" +
                    "WHERE \n" +
                    "t1.ID < 1\n" +
                    "OR t2.ID < 1\n" +
                    "OR t3.ID < 1\n" +
                    "OR t4.ID < 1\n" +
                    "OR t5.ID < 1\n" +
                    "OR t6.ID < 1\n" +
                    ";\n");
            new Thread(()->{
               try {
                   Thread.sleep(500);
                   statement.close();
                   conn.close();
               }catch (Exception e){
                   e.printStackTrace();
               }
            }).start();
            statement.execute();
            ResultSet resultSet  = statement.getResultSet();
            while (resultSet.next()){
                System.out.println(resultSet.getString("ID"));
            }

        } catch (Exception throwables) {
            throwables.printStackTrace();
        }


    }



    public static void main3(String[] args) {
        try {
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url, usr, pwd);
            // ResultSet resultSet = conn.getMetaData().getColumns(null, "HELLO_WORLD", "HELLO_WORLD", "%");
            ResultSet resultSet = conn.getMetaData().getColumns(null, "HELLO_WORLD", "XXXXXAAAA", "%");
            while (resultSet.next()){
                System.out.println(resultSet.getString("COLUMN_NAME"));
            }

        } catch (Exception throwables) {
            throwables.printStackTrace();
        }


    }

    public static void main2(String[] args) {
        try {
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url, usr, pwd);
            ResultSet resultSet = conn.getMetaData().getSchemas();
            ResultSetMetaData rsmd = resultSet.getMetaData();
            int columnSize = rsmd.getColumnCount();
            List<Map<String, String>> title = new ArrayList<>();

            for (int i = 1; i <= columnSize; i++) {
                Map<String, String> row = new HashMap<String, String>();
                if (i == 1) {
                    row.put("fixed", "left");
                }
                row.put("key", rsmd.getColumnLabel(i));
                row.put("title", rsmd.getColumnLabel(i));
                // row.put(DISPLAY_WIDTH, "200");
                title.add(row);
                System.out.println(row);
            }
            // Parsing data.

            List<Map<String, String>> data = new ArrayList<>();

            while (resultSet.next()) {
                Map<String, String> row = new HashMap<String, String>();
                for (int i = 1; i <= columnSize; i++) {
                    row.put(title.get(i - 1).get("key"), resultSet.getString(i));
                }
                data.add(row);
            }
            System.out.println(data);
            System.out.println(data.size());
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }


    }



    @Test
    public void testClose(){
        try {
            HikariConfig config = new HikariConfig();

            config.setJdbcUrl(url);
            config.setUsername(usr);
            config.setPassword(pwd);
            HikariDataSource dataSource = new HikariDataSource(config);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
